package com.shop.seller.service.impl;


import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.domain.GoodsQueryVo;
import com.shop.domain.PageResult;
import com.shop.mapper.BrandMapper;
import com.shop.mapper.GoodsMapper;
import com.shop.mapper.Goods_descMapper;
import com.shop.mapper.ItemCatMapper;
import com.shop.mapper.ItemMapper;
import com.shop.mapper.SellerMapper;
import com.shop.model.Brand;
import com.shop.model.Goods;
import com.shop.model.GoodsExample;
import com.shop.model.GoodsExample.Criteria;
import com.shop.model.Goods_desc;
import com.shop.model.Item;
import com.shop.model.ItemCat;
import com.shop.model.Seller;
import com.shop.seller.service.GoodsService;


/**
 * 
 * @author Y
 *
 */
@Service("goodsService")
/*@Transactional*/
public class GoodsServiceBean implements GoodsService {

	@Resource
	private GoodsMapper goodsMapper;
	@Resource
	private Goods_descMapper goods_descMapper;
	@Resource
	private ItemMapper itemMapper;
	@Resource
	private ItemCatMapper itemCatMapper;
	@Resource
	private BrandMapper brandMapper;
	@Resource
	private SellerMapper sellerMapper;
	@Override
	public PageResult findPage(Goods goods, Integer pageNum, Integer pageSize) throws Exception {		
		PageHelper.startPage(pageNum, pageSize);//分页
		GoodsExample goodsExample = new GoodsExample();
		Criteria criteria = goodsExample.createCriteria();
		if (goods != null) { // 拼接正则
			if (goods.getSellerId() != null && goods.getSellerId().length() > 0) {
				criteria.andSellerIdEqualTo(goods.getSellerId());
			}		
			if(goods.getGoodsName()!=null&& goods.getGoodsName().length()>0){
				System.out.println(goods.getGoodsName());
				criteria.andGoodsNameLike("%"+goods.getGoodsName()+"%");	
			}
			if(goods.getAuditStatus()!=null&&goods.getAuditStatus().length()>0){
				criteria.andAuditStatusEqualTo(goods.getAuditStatus());
			}
			if(goods.getIsMarketable()!=null&&goods.getIsMarketable().length()>0){
				criteria.andIsMarketableEqualTo(goods.getIsMarketable());
			}
		}
		// 拿到page分页对象
		Page<Goods> page=(Page<Goods>) goodsMapper.selectByExample(goodsExample);
		return new PageResult(page.getTotal(), page.getResult());// long List
	}

	@Override
	public boolean deleteGoods(Long id) throws Exception {
		
		int count=goodsMapper.deleteByPrimaryKey(id);
		if(count>0){
		return true;
		}
		else{
			
			return false;
		}
	}

	@Override
	public boolean updateGoods(Goods goods) throws Exception {
		
		
		int count=goodsMapper.updateByPrimaryKeySelective(goods);
		
		return count>0;
	}

	@Override
	public Goods findGoodsById(Long id) throws Exception {
		
		//查询不到返回null
		Goods goods=goodsMapper.selectByPrimaryKey(id);
		
		return goods;
	}

	@Override
	public void addGoods(GoodsQueryVo goodsVo) throws Exception {
		//商品基本信息pojo
		Goods goods=goodsVo.getCustomGoods();
		
		//商品描述信息pojo
		Goods_desc goodsdesc=goodsVo.getCustomGoodsdesc();
		
		//商品SKU集合
		List<Item> items=goodsVo.getItemLst();

     	//设置默认上架状态为未上架
		goods.setIsMarketable("0");
		
     	//设置删除状态为未删除
		goods.setIsDelete("0");
		
     	//设置默认商品审核状态为未审核
		goods.setAuditStatus("0");
		
     	//插入商品基本信息并返回主键
        goodsMapper.insert(goods);
        System.out.println("goodsid="+goods.getId());
     	//把插入商品的主键赋给商品描述信息当做ID
          goodsdesc.setGoodsId(goods.getId());
     	//插入商品描述信息
         goods_descMapper.insert(goodsdesc);
     	//判断是否启用规格,1为启用
     	if("1".equals(goods.getIsEnableSpec()))
     	{
     		
     		for(Item item:items){
     			//构建标题 SPU名+规格选项值
         		String title=goods.getGoodsName();//spu名
     			//把json字符串转成Map集合
     			Map<String,Object> map=JSON.parseObject(item.getSpec());
     			//循环取出选项名拼成title
     			for(String key:map.keySet())
     			{
     				title+=" "+map.get(key);
     			}
     			item.setTitle(title);//赋值给sku的title
     			setItemValue(goods,goodsdesc,item);
     			itemMapper.insert(item);
     		}
     	}else{
     		Item item=new Item();
     		item.setTitle(goods.getGoodsName());//商品名
     		item.setPrice(goods.getPrice());//价格
     		item.setNum(9999);//库存
     		item.setStatus("1");//状态
     		item.setIsDefault("1");//默认
     		setItemValue(goods,goodsdesc,item);
     		itemMapper.insert(item);
     	}
		
		
	}
    //给item设值
	private void setItemValue(Goods goods,Goods_desc goodsdesc,Item item){
		
 			item.setCategoryid(goods.getCategory3Id());	//商品三级分类id
 			item.setCreateTime(new Date());//创建日期
 			item.setUpdateTime(new Date());//修改日期
 			item.setGoodsId(goods.getId());//商品id
 			item.setSeller(goods.getSellerId());//商家id
 			ItemCat itemCat=itemCatMapper.selectByPrimaryKey(goods.getCategory3Id());//查出三级分类名
 		    item.setCategory(itemCat.getName());//三级分类名
 		    Brand brand= brandMapper.selectByPrimaryKey(goods.getBrandId());//查出品牌名
 		    item.setBrand(brand.getName());//品牌名
 		    Seller seller=sellerMapper.selectByPrimaryKey(goods.getSellerId());
 		    item.setSeller(seller.getName());//商家名
 		    List<Map> images=JSON.parseArray(goodsdesc.getItemImages(),Map.class);
 		    if(images.size()>0)
 		    {
 		    	item.setImage((String)images.get(0).get("url"));//图片
 		    }
 		    
 		}
 	
	}

